<template>
	<view class="goods-item">
		<!-- 左侧 -->
		<view class="goods-item-left">
			<radio :checked="goods.goods_state" color="#C00000" v-if="showRadio" @click="radioclickHandler"></radio>
			<image class="goods-pic" :src="goods.goods_small_logo || defaultPic"></image>
		</view>
		<!-- 右侧 -->
		<view class="goods-item-right">
			<view class="goods-name">{{goods.goods_name}}</view>
			<view class="goods-info-box">
				<view class="goods-price">
					￥{{goods.goods_price | toFixed}}
				</view>
				<uni-number-box v-if="showNum" :min="1" :value="goods.goods_count" @change="numberchangeHandler"></uni-number-box>
			</view>
		</view>
	</view>
</template>


<script>
	export default {
		props: {
			goods: {
				type: Object,
				default: {}
			},
			showRadio: {
				type: Boolean,
				default: false
			},
			showNum: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				defaultPic: 'https://img.zcool.cn/community/015d265b44c01ba80121ade095d2f7.jpg@2o.jpg'
			}
		},
		onLoad() {

		},

		methods: {


			//单选框的点击事件处理函数
			// radioclickHandler(){
			// 	this.$emit('radio-change',{
			// 		goods_id:this.goods.goods_id,
			// 		goods_state: !this.goods.goods_state
			// 	})
			// }

			//Vue 中使用 v-model 绑定的数据是响应式的，直接对其进行取反操作可能无法生效，因为 Vue 会维护数据的状态，确保其与视图的同步。
			radioclickHandler() {
				this.$set(this.goods, 'goods_state', !this.goods.goods_state);
				this.$emit('radio-change', {
					goods_id: this.goods.goods_id,
					goods_state: this.goods.goods_state
				});
			},
			
			numberchangeHandler(val){
				this.$emit('num-change',{
					goods_id:this.goods.goods_id,
					goods_count: +val
				})
			}
		},
		//使用过滤器处理价格
		filters: {
			toFixed(num) {
				return Number(num).toFixed(2)
			}
		}

	}
</script>


<style lang="scss">
	.goods_list {}

	.goods-item {
		width: 750rpx;
		box-sizing: border-box;
		display: flex;
		padding: 10px 5px;
		border-bottom: 1px solid #f0f0f0;

		.goods-item-left {
			margin-right: 5px;
			display: flex;
			justify-content: space-between;
			align-items: center;


			.goods-pic {
				width: 100px;
				height: 100px;
				display: block;
			}
		}

		.goods-item-right {
			display: flex;
			flex: 1;
			flex-direction: column;
			justify-content: space-between;

			.goods-name {
				font-size: 13px;
			}

			.goods-info-box {
				display: flex;
				justify-content: space-between;
				align-items: center;
				
				.goods-price {
					font-size: 16px;
					color: #C00000;
				}
			}
		}
	}
</style>